<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>会议信息</title>
    <link rel="stylesheet" href="/static/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
    <style>
        .layui-badge {
            height: 30px;
            line-height: 30px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <div class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">会议类型：</label>
                            <div class="layui-input-inline">
                                <select id="typeValue">
                                    <option value=""></option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">

                            <label class="layui-form-label">会议名称：</label>
                            <div class="layui-input-inline">
                                <input type="text" id="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">主持人：</label>
                            <div class="layui-input-inline">
                                <input type="text" id="empName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">部门：</label>
                            <div class="layui-input-inline">
                                <input type="text" id="deptName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">会议时间：</label>
                            <div class="layui-input-inline">
                                <input type="datetime-local" id="startTime" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-form-mid">至</div>
                            <div class="layui-input-inline">
                                <input type="datetime-local" id="endTime" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button class="layui-btn layui-btn-primary"
                                    data-type="reloads"><i class="layui-icon"></i> 检 索
                            </button>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn layui-btn-primary"
                                    data-type="clearsheack">&nbsp;清&nbsp;&nbsp;空&nbsp;
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </fieldset>

        <script type="text/html" id="meetTool">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
                <button class="layui-btn layui-btn-sm layui-btn-normal data-delete-btn" lay-event="flush"> 刷新</button>
            </div>
        </script>

        <table class="layui-hide" id="meetTable" lay-filter="meetTableFilter"></table>

        <script type="text/html" id="meetTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="/static/lib/layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        //初始化表格加载
        var tableId = table.render({
            elem: '#meetTable',
            url: '/meet/list',
            toolbar: '#meetTool',
            defaultToolbar: ['filter', 'exports', 'print',],
            response: {
                statusCode: 200 //规定成功的状态码，默认：0
                , countName: 'total' //规定数据总数的字段名称，默认：count
            },
            cols: [
                [
                    {type: "checkbox", width: '4%'},
                    {field: 'name', width: '8%', title: '会议名称', align: "center"},
                    {field: 'typeName', width: '8%', title: '会议类型', align: "center"},
                    {field: 'startTime', width: '10%', title: '开始时间', align: "center"},
                    {field: 'endTime', width: '10%', title: '结束时间', align: "center"},
                    {field: 'deptName', width: '8%', title: '发起部门', align: "center"},
                    {field: 'empName', width: '6%', title: '主持人', align: "center"},
                    {field: 'count', width: '6%', title: '参会人数', align: "center"},
                    {
                        field: 'status', width: '6%', title: '状态', align: "center", templet: function (res) {
                            if (res.status == 1) {
                                return '<span class="layui-badge layui-bg-orange">未开始</span>'
                            } else if (res.status == 2) {
                                return '<span class="layui-badge layui-bg-green" >进行中</span>'
                            } else {
                                return '<span class="layui-badge layui-bg-red" >已结束</span>'
                            }
                        }
                    },
                    {field: 'createName', width: '6%', title: '创建人', align: "center"},
                    {field: 'createDate', width: '10%', title: '创建时间', align: "center"},
                    {title: '操作', minWidth: 150, toolbar: '#meetTableBar', align: "center"}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 20,
            page: true,
            id: 'meetTableId',
            skin: 'line'
        });

        var active = {
            //执行搜索重载
            reloads: function () {
                var typeValue = $('#typeValue');          //会议类型
                var name = $('#name');                  //会议名称
                var empName = $('#empName');            //主持人名称
                var deptName = $('#deptName');          //部门名称
                var startTime = $('#startTime');        //会议开始时间
                var endTime = $('#endTime');            //会议结束时间

                //执行重载
                tableId.reload({
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        "typeValue": typeValue.val(),
                        "name": name.val(),
                        "empName": empName.val(),
                        "deptName": deptName.val(),
                        "startTime": startTime.val(),
                        "endTime": endTime.val(),
                    }
                }, 'data');
            },
            //清空重载
            clearsheack: function () {
                var typeValue = $('#typeValue');          //会议类型
                var name = $('#name');                  //会议名称
                var empName = $('#empName');            //主持人名称
                var deptName = $('#deptName');          //部门名称
                var startTime = $('#startTime');        //会议开始时间
                var endTime = $('#endTime');            //会议结束时间

                typeValue.val("");
                name.val("");
                empName.val("");
                deptName.val("");
                startTime.val("");
                endTime.val("");

                tableId.reload({
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        "typeValue": "",
                        "name": "",
                        "empName": "",
                        "deptName": "",
                        "startTime": "",
                        "endTime": "",
                    }
                }, 'data');
            },
            //下拉框选择
            selectClass:function(){
                var typeValue = $('#typeValue');
                $.ajax({
                    type: "get",
                    url: "/dictionaries/type/meetType",
                    dataType: "json",
                    traditional: true,
                    success: function (res) {
                        var data = res.data;
                        data.forEach(item=>
                            typeValue.append(new Option(item.name,item.value))
                        );
                        form.render();
                    }
                });
            }
        };
        active.selectClass();   //执行一次下拉框数据获取

        /**
         * toolbar监听事件
         */
        table.on('toolbar(meetTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var open = layer.open({
                    title: '添加会议信息',
                    type: 2,
                    shadeClose: true,
                    scrollbar: false,
                    area: ['40%', '70%'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    content: '/meet/show',
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.layui.form.on("submit(save)", function (data) {
                            var files = data.field;
                            files['empIds'] = iframeWin.empIds;
                            files['deptIds'] = iframeWin.deptIds;
                            files['typeName'] = iframeWin.typeName;
                            files['roomName'] = iframeWin.roomName;
                            $.ajax({
                                type: "post",
                                url: '/meet/save',
                                dataType: "json",
                                traditional: true,
                                data: files,
                                success: function (data) {
                                    layer.closeAll();
                                    layer.msg(data.msg);
                                    active.reloads();
                                },
                                error: function (data) {
                                    layer.closeAll();
                                    layer.msg("错误！请联系管理员");
                                }
                            });
                            return false;
                        });
                        body.find("#save").click();
                    }
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('meetTableId')
                    , data = checkStatus.data;
                if (data.length <= 0) {
                    layer.msg("未选中任何行");
                    return;
                } else {
                    var ids = [];
                        ids[0] = data[0].id;
                    var names = data[0].name;
                    for (var i = 1; i < data.length; i++) {
                        ids[i] = data[i].id;
                        names += "," + data[i].name;
                    }
                    layer.confirm('确认删除:' + names, function (index) {
                        $.ajax({
                            type: "delete",
                            url: "/meet/delete",
                            dataType: "json",
                            traditional: true,
                            data: {"ids": ids},
                            success: function (data) {
                                layer.closeAll();
                                layer.msg(data.msg);
                                active.reloads();
                            },
                            error: function (data) {
                                layer.closeAll();
                                layer.msg(data);
                            }
                        });
                        layer.close(index);
                    });
                }
            } else if (obj.event === 'flush') {  // 监听刷新操作
                active.reloads();
                layer.msg("刷新成功");
            }
        });

        table.on('tool(meetTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                //编辑会议信息
                var open = layer.open({
                    title: '编辑会议信息',
                    type: 2,
                    shadeClose: true,
                    scrollbar: false,
                    area: ['40%', '70%'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    content: '/meet/show?id=' + data.id,
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.layui.form.on("submit(save)", function (fromdata) {
                            var files = fromdata.field;
                            files['id'] = data.id;
                            files['empIds'] = iframeWin.empIds;
                            files['deptIds'] = iframeWin.deptIds;
                            files['typeName'] = iframeWin.typeName;
                            files['roomName'] = iframeWin.roomName;
                            $.ajax({
                                type: "put",
                                url: "/meet/edit",
                                dataType: "json",
                                traditional: true,
                                data: files,
                                success: function (data) {
                                    layer.closeAll();
                                    layer.msg(data.msg);
                                    active.reloads();
                                },
                                error: function (data) {
                                    layer.closeAll();
                                    layer.msg("错误！请联系管理员");
                                }
                            });
                            return false;
                        });
                        body.find("#save").click();
                    }
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        type: "delete",
                        url: "/meet/delete",
                        dataType: "json",
                        data: {
                            'ids': data.id
                        },
                        success: function (data) {
                            if (data.success) {
                                layer.closeAll();
                                layer.msg(data.msg);
                                obj.del();
                            } else {
                                layer.msg(data.msg);
                            }
                        },
                        error: function (data) {
                            layer.closeAll();
                            layer.msg(data);
                        }
                    });
                    layer.close(index);
                });
            }
        });

        //加载自定义方法
        $('[data-type]').click(function () {
            var type = $(this).data('type');
            active[type].call(this);
        })

    });

</script>

</body>
</html>